package com.itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.itheima.reggie.entity.Employee;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.LocalTime;

@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 插入操作 更新字段
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {

        log.info("公共字段填充：[insert]");
        log.info(metaObject.toString());
        long id = Thread.currentThread().getId();
        log.info("该线程的ID为：{}",id);

        metaObject.setValue("createTime", LocalDateTime.now());

        metaObject.setValue("updateTime", LocalDateTime.now());

        metaObject.setValue("createUser", BaseContext.getCurrentId());

        metaObject.setValue("updateUser", BaseContext.getCurrentId());

    }

    /**
     * 更新操作 更新字段
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {

        log.info("公共字段填充：[update]");
        log.info(metaObject.toString());

        long id = Thread.currentThread().getId();
        log.info("该线程的ID为：{}",id);

        metaObject.setValue("updateTime", LocalDateTime.now());

        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }
}
